package com.tamo.ppresta.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.tamo.ppresta.R;
import com.tamo.ppresta.base.BaseActivity;
import com.tamo.ppresta.bean.GetMineInfoBean;
import com.tamo.ppresta.bean.GetRecommendItemsBean;
import com.tamo.ppresta.bean.UserMineInfoEntity;
import com.tamo.ppresta.constant.APP_CONSTANT;
import com.tamo.ppresta.constant.SPConstant;
import com.tamo.ppresta.greendao.DBManager;
import com.tamo.ppresta.util.ButtonUtils;
import com.tamo.ppresta.util.GlideUtils;
import com.tamo.ppresta.util.LogUtils;
import com.tamo.ppresta.util.MyUtils;
import com.tamo.ppresta.util.SPUtil;
import com.tamo.ppresta.util.ThirdApp;
import com.tamo.ppresta.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author yeyang
 * @name ppresta
 * @class name：com.tamo.ppresta.activity
 * @class describe
 * @time 2022/12/31 14:42
 * @change
 * @chang time
 * @class 产品详情
 */
public class ProductoDetailActivity extends BaseActivity {

    @BindView(R.id.tv_producto_tips)
    TextView mTvProductoTips;
    @BindView(R.id.tv_enviar)
    TextView mTvEnviar;
    @BindView(R.id.iv_gongzuoxixi)
    ImageView mIvGongzuoxixi;
    @BindView(R.id.iv_gongzuoxixi_gougou)
    ImageView mIvGongzuoxixiGougou;
    @BindView(R.id.iv_gongzuoxixi_arrow)
    ImageView mIvGongzuoxixiArrow;
    @BindView(R.id.rl_weirenzheng2)
    RelativeLayout mRlWeirenzheng2;
    @BindView(R.id.tv_gongzuoxixi)
    TextView mTvGongzuoxixi;
    @BindView(R.id.cv3)
    CardView mCv3;
    @BindView(R.id.iv_lianxirenxinxi)
    ImageView mIvLianxirenxinxi;
    @BindView(R.id.iv_lianxirenxinxi_gougou)
    ImageView mIvLianxirenxinxiGougou;
    @BindView(R.id.iv_lianxirenxinxi_arrow)
    ImageView mIvLianxirenxinxiArrow;
    @BindView(R.id.rl_weirenzheng3)
    RelativeLayout mRlWeirenzheng3;
    @BindView(R.id.tv_lianxirenxinxi)
    TextView mTvLianxirenxinxi;
    @BindView(R.id.cv4)
    CardView mCv4;
    @BindView(R.id.iv_shenfenxinxi)
    ImageView mIvShenfenxinxi;
    @BindView(R.id.iv_shenfenxinxi_gougou)
    ImageView mIvShenfenxinxiGougou;
    @BindView(R.id.iv_shenfenxinxi_arrow)
    ImageView mIvShenfenxinxiArrow;
    @BindView(R.id.rl_weirenzheng4)
    RelativeLayout mRlWeirenzheng4;
    @BindView(R.id.tv_shenfenxinxi)
    TextView mTvShenfenxinxi;
    @BindView(R.id.cv5)
    CardView mCv5;
    @BindView(R.id.iv_app_icon)
    ImageView mIvAppIcon;
    @BindView(R.id.tv_app_name)
    TextView mTvAppName;
    @BindView(R.id.rl_app_info)
    RelativeLayout mRlAppInfo;
    @BindView(R.id.tv_app_hint1)
    TextView mTvAppHint1;
    @BindView(R.id.tv_app_money1)
    TextView mTvAppMoney1;
    @BindView(R.id.rl_app_info_hint1)
    RelativeLayout mRlAppInfoHint1;
    @BindView(R.id.tv_app_hint2)
    TextView mTvAppHint2;
    @BindView(R.id.tv_app_money2)
    TextView mTvAppMoney2;
    @BindView(R.id.rl_app_info_hint2)
    RelativeLayout mRlAppInfoHint2;
    @BindView(R.id.tv_app_hint3)
    TextView mTvAppHint3;
    @BindView(R.id.tv_app_money3)
    TextView mTvAppMoney3;
    @BindView(R.id.rl_app_info_hint3)
    RelativeLayout mRlAppInfoHint3;
    @BindView(R.id.tv_app_hint4)
    TextView mTvAppHint4;
    @BindView(R.id.tv_app_money4)
    TextView mTvAppMoney4;
    @BindView(R.id.rl_app_info_hint4)
    RelativeLayout mRlAppInfoHint4;
    @BindView(R.id.tv_app_hint5)
    TextView mTvAppHint5;
    @BindView(R.id.tv_app_money5)
    TextView mTvAppMoney5;
    @BindView(R.id.rl_app_info_hint5)
    RelativeLayout mRlAppInfoHint5;
    @BindView(R.id.view_line)
    View mViewLine;
    @BindView(R.id.cv1)
    CardView mCv1;
    @BindView(R.id.iv_gerenxinxi)
    ImageView mIvGerenxinxi;
    @BindView(R.id.iv_gerenxinxi_gougou)
    ImageView mIvGerenxinxiGougou;
    @BindView(R.id.iv_gerenxinxi_arrow)
    ImageView mIvGerenxinxiArrow;
    @BindView(R.id.rl_weirenzheng1)
    RelativeLayout mRlWeirenzheng1;
    @BindView(R.id.tv_gerenxinxi)
    TextView mTvGerenxinxi;
    @BindView(R.id.cv2)
    CardView mCv2;
    @BindView(R.id.iv_title_right)
    ImageView mIvTitleRight;
    @BindView(R.id.rl_title_right)
    RelativeLayout mRlTitleRight;
    @BindView(R.id.iv_title_back)
    ImageView mIvTitleBack;
    @BindView(R.id.rl_title_back)
    RelativeLayout mRlTitleBack;
    private GetRecommendItemsBean.RecommendItemsEntity mRecommendItemsEntity;
    private GetMineInfoBean mGetMineInfoBean;
    private PopupWindow mPopupWindow;
    /**
     * 个人信息认证
     */
    private boolean is_PERSON_RZ;
    /**
     * 工作信息认证
     */
    private boolean is_WORK_RZ;
    /**
     * 联系人认证
     */
    private boolean is_CONTACTS_RZ;
    /**
     * 证件认证
     */
    private boolean is_CARD_RZ;
    /**
     * 人脸认证
     */
    private boolean is_FACE_RZ;
    /**
     * 银行卡认证
     */
    private boolean is_BANK_RZ;

    @Override
    protected int setLayout() {
        return R.layout.activity_producto_detail;
    }

    @Override
    protected void setView() {
        mRlTitleBack.setOnClickListener(this::onClick);
        mRlTitleRight.setOnClickListener(this::onClick);
        mCv2.setOnClickListener(this::onClick);
        mCv3.setOnClickListener(this::onClick);
        mCv4.setOnClickListener(this::onClick);
        mCv5.setOnClickListener(this::onClick);
        mTvEnviar.setOnClickListener(this::onClick);
    }

    @Override
    protected void setData() {
        mRecommendItemsEntity = (GetRecommendItemsBean.RecommendItemsEntity) getIntent().getSerializableExtra(APP_CONSTANT.DATA);
        mGetMineInfoBean = DBManager.getInstance(getApplicationContext()).queryMineInfoBean();
        LogUtils.info(TAG, mGetMineInfoBean.toString());
        if (null != mRecommendItemsEntity) {
            if (null != mRecommendItemsEntity.getProductLogoUrl()) {
                GlideUtils.showImageView(this, R.mipmap.icon_start, mRecommendItemsEntity.getProductLogoUrl(), mIvAppIcon);
            }
            if (null != mRecommendItemsEntity.getProductName()) {
                mTvAppName.setText(mRecommendItemsEntity.getProductName());
            }
            if (null != mRecommendItemsEntity.getMaxAmount()) {
                mTvAppMoney1.setText("$" + mRecommendItemsEntity.getMaxAmount());
            }
            if (null != mRecommendItemsEntity.getShowLoanDay()) {
                mTvAppMoney2.setText(mRecommendItemsEntity.getShowLoanDay());
            }
            mTvAppMoney3.setText("$" + mRecommendItemsEntity.getActualToAccountMoney());
            mTvAppMoney4.setText("$" + mRecommendItemsEntity.getServiceFee());
            mTvAppMoney5.setText("$" + mRecommendItemsEntity.getMaxRepayAmount());
        }

        // 0 未认证，1  已认证
        if (null != mGetMineInfoBean) {
            if (null != mGetMineInfoBean.getUserMineInfo()) {
//                GetMineInfoBean.UserMineInfoEntity userMineInfo = mGetMineInfoBean.getUserMineInfo();
                UserMineInfoEntity userMineInfo = mGetMineInfoBean.getUserMineInfo();
                if (userMineInfo.getPersonalCredit().equals("1")) {
                    is_PERSON_RZ = true;
                    mRlWeirenzheng1.setVisibility(View.GONE);
                    mIvGerenxinxiGougou.setVisibility(View.VISIBLE);
                } else {
                    mRlWeirenzheng1.setVisibility(View.VISIBLE);
                    mIvGerenxinxiGougou.setVisibility(View.GONE);
                }
                if (userMineInfo.getWorkCredit().equals("1")) {
                    is_WORK_RZ = true;
                    mRlWeirenzheng2.setVisibility(View.GONE);
                    mIvGongzuoxixiGougou.setVisibility(View.VISIBLE);
                } else {
                    mRlWeirenzheng2.setVisibility(View.VISIBLE);
                    mIvGongzuoxixiGougou.setVisibility(View.GONE);
                }
                if (userMineInfo.getContactCredit().equals("1")) {
                    is_CONTACTS_RZ = true;
                    mRlWeirenzheng3.setVisibility(View.GONE);
                    mIvLianxirenxinxiGougou.setVisibility(View.VISIBLE);
                } else {
                    mRlWeirenzheng3.setVisibility(View.VISIBLE);
                    mIvLianxirenxinxiGougou.setVisibility(View.GONE);
                }
                if (userMineInfo.getIdentityCredit().equals("1")) {
                    is_CARD_RZ = true;
                    mRlWeirenzheng4.setVisibility(View.GONE);
                    mIvShenfenxinxiGougou.setVisibility(View.VISIBLE);
                } else {
                    mRlWeirenzheng4.setVisibility(View.VISIBLE);
                    mIvShenfenxinxiGougou.setVisibility(View.GONE);
                }
                if (userMineInfo.getBankCredit().equals("1")) {
                    is_BANK_RZ = true;
                }
                if (userMineInfo.getFaceCredit().equals("1")) {
                    is_FACE_RZ = true;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        ButtonUtils.isFastDoubleClick();
        switch (v.getId()) {
            case R.id.rl_title_back:
                finish();
                break;
            case R.id.rl_title_right:
                showKeFuPopWindow();
                break;
            case R.id.cv2:
                ToastUtil.showShort(getApplicationContext(), "个人信息认证");
                break;
            case R.id.cv3:
                ToastUtil.showShort(getApplicationContext(), "工作信息信息认证");
                break;
            case R.id.cv4:
                ToastUtil.showShort(getApplicationContext(), "联系人信息认证");
                break;
            case R.id.cv5:
                ToastUtil.showShort(getApplicationContext(), "身份信息认证");
                break;
            case R.id.tv_enviar:
                //// TODO: 2023/1/1 先判断是否都认证了
                toClass(this,PassTheAuditActivity.class);
                if (!is_PERSON_RZ) {
                    LogUtils.info(TAG, "个人信息未认证，跳转页面");
                    ToastUtil.showShort(getApplicationContext(), "个人信息未认证，跳转页面");
                }
                if (!is_WORK_RZ) {
                    LogUtils.info(TAG, "工作信息未认证，跳转页面");
                    ToastUtil.showShort(getApplicationContext(), "工作信息未认证，跳转页面");
                }
                if (!is_CONTACTS_RZ) {
                    LogUtils.info(TAG, "联系人未认证，跳转页面");
                    ToastUtil.showShort(getApplicationContext(), "联系人未认证，跳转页面");
                }
                if (!is_CARD_RZ) {
                    LogUtils.info(TAG, "证件未认证，跳转页面");
                    ToastUtil.showShort(getApplicationContext(), "证件未认证，跳转页面");
                }
                if (!is_FACE_RZ) {
                    LogUtils.info(TAG, "人脸未认证，跳转页面");
                    ToastUtil.showShort(getApplicationContext(), "人脸未认证，跳转页面");
                }
                if (!is_BANK_RZ) {
                    LogUtils.info(TAG, "银行卡未认证，跳转页面");
                    ToastUtil.showShort(getApplicationContext(), "银行卡未认证，跳转页面");
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setStatusBar();
        ButterKnife.bind(this);
    }

    private void setStatusBar() {
        MyUtils.setStatusBarVisible(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .transparentStatusBar()
                .statusBarColor(R.color.color_6981f1)
                .statusBarDarkFont(false)
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                .keyboardEnable(true)
                .init();
    }

    /**
     * 展示客服弹窗
     */
    private void showKeFuPopWindow() {
        // 1、获取用于 PopupWindow 的弹窗view
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.pop_kefutankuang, null);
        View layout = layoutInflater.inflate(R.layout.activity_producto_detail, null);

        // 2、创建 PopupWindow 实例，传入 View 及宽高
        mPopupWindow = new PopupWindow(customView, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setOutsideTouchable(false);

        // 3、展示
        mPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
        MyUtils.backgroundAlpha(this, 0.5f);

        customView.findViewById(R.id.rl_kefutankuang_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                MyUtils.backgroundAlpha(ProductoDetailActivity.this, 1f);
            }
        });
        TextView tvPhone1 = customView.findViewById(R.id.tv_phone_number1);
        TextView tvPhone2 = customView.findViewById(R.id.tv_phone_number2);
        customView.findViewById(R.id.iv_wechat1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.chatInWhatsApp(ProductoDetailActivity.this, tvPhone1.getText().toString());
            }
        });
        customView.findViewById(R.id.iv_phone1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.callPhone(ProductoDetailActivity.this, tvPhone1.getText().toString());
            }
        });
        customView.findViewById(R.id.iv_wechat2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.chatInWhatsApp(ProductoDetailActivity.this, tvPhone2.getText().toString());
            }
        });
        customView.findViewById(R.id.iv_phone2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.callPhone(ProductoDetailActivity.this, tvPhone1.getText().toString());
            }
        });
    }
}